var datagrid = null;
$(function() {
	datagrid = $('#datagrid').datagrid({
		url : host + 'tableJoinAction/getTableJoinList.html?tableName=' + baseJs.request("tableName"),
		fit : true,
		fitColumns : true,
		border : false,
		pagination : false,
		checkOnSelect : false,
		selectOnCheck : false,
		rownumbers: true,
		nowrap : false,
		frozenColumns: [],
		columns : [[
			{field : 'tableName1', title : '表名', width : 60},
			{field : 'fieldName1', title : '字段名称', width : 60},
			{field : 'tableName2', title : '关联表', width : 60},
			{field : 'fieldName2', title : '关联字段', width : 60},
			{field : 'id', title : '操作', width : 100,
				formatter : function(value, row, index) {
					return "<a href='javascript: void(0)' onclick='editJoinTable(" + index + ")'>修改</a>&nbsp;&nbsp;"+
							"<a href='javascript: void(0)' onclick='deleteJoinTable(\"" + value + "\")'>删除</a>"; 
				}
			}
		]],
		toolbar: [{
			text : '新增',
			iconCls : 'icon-add',
			handler : function() {
				editJoinTable();
			}
		}, {
			text : '关闭',
			iconCls : 'icon-cancel',
			handler : function() {
				baseJs.closeWindow();
			}
		}]
	});
	$("#open-dlg").dialog({});
	initEditData();
});
var map = {};
function initEditData() {
	$.post(host + 'tableAction/getTableList.html', {}, function(data) {
		data = data.rows;
		var nodes = [];
		for (var i = 0;i < data.length;i ++) {
			map[data[i].id] = {id: data[i].name, text: data[i].text};
			if (map[data[i].parentId] != undefined) {
				if (map[data[i].parentId].children == undefined) {
					map[data[i].parentId].children = [];
					map[data[i].parentId].state = "closed";
				}
				map[data[i].parentId].children.push(map[data[i].id]);
			} else {
				nodes.push(map[data[i].id]);
			}
		}
		$('#tableName2').combotree({
			data: nodes,
			textField: 'name',
			valueField: 'name',
			onChange: function(newValue, oldValue) {
				initField("fieldName2", newValue);//map[newValue].name);
			}
		});
	}, "json");
	$("#fieldName1").combobox({
		textField: 'name',
		valueField: 'name'
	});
	$("#fieldName2").combobox({
		textField: 'name',
		valueField: 'name'
	});
	initField("fieldName1", baseJs.request("tableName"));
	$("#tableName1").html(baseJs.request("name"));
}
function initField(id, tableName) {
	$.post(host + 'tableAction_getFieldList.do', {tableName: tableName}, function(data) {
		$("#" + id).combobox("loadData", data);
		$("#" + id).combobox("setValue", "");
	}, "json");
}
function saveData() {
	var json = {
		"join.tableName1": baseJs.request("tableName"),
		"join.fieldName1": $("#fieldName1").combobox("getValue"),
		"join.tableName2": $("#tableName2").combotree("getValue"),
		"join.fieldName2": $("#fieldName2").combobox("getValue"),
		"join.joinType": $("#joinType").val(),
		"join.isNotNull": $("#isNotNull").val()
	};
	var id = baseJs.request("id");
	if (id) {
		json["join.id"] = id;
	}
	$.post(host + "tableJoinAction/save.html", json, function(result) {
		alert(result.msg);
		if (result.success) {
			closeDialog();
			$('#datagrid').datagrid("reload");
		}
	}, "json");
}
function editTable(index) {
	if (id != undefined) {
		var data = 
		$("#id").val(id);
		$("#fieldName1").combobox("getValue")
	} else {
		$("#id").val("");
		$("#fieldName1").combobox("");
		$("#tableName2").combobox("");
		$("#fieldName2").combobox("");
		$("#joinType").val("OneToOne");
		$("#isNotNull").val("1");
	}
	$('#open-dlg').dialog('open').dialog('setTitle', '编辑关联表信息');
}
function deleteJoinTable() {
	
}
function closeDialog() {
	$('#open-dlg').dialog('close');
}